package per.wxp.elasticsearchDemo.web;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import per.wxp.elasticsearchDemo.Service.ElasticSearchService;

import java.io.IOException;

@RestController
@RequestMapping("/ElasticSearch")
public class IndexController {

    @Autowired
    private ElasticSearchService elasticSearchService;


    @GetMapping("/")
    public String index(){

        return  "index";
    }


    /*
     *创建索引
     */


    /**
     * 根据名字创建
     * @param index
     * @return
     */
    @PutMapping("/{indexName}")
    @ApiOperation(value="创建索引", notes="index 索引名称")
    public ResponseEntity CreateIndex(@PathVariable("indexName") String index){
        elasticSearchService.createIndex(index);
        return  new ResponseEntity( HttpStatus.OK);
    }

    /**
     *创建 index 和 type
     */
    @ApiOperation(value="创建索引", notes="index 索引名称")
   // @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PutMapping("/{index}/{type}")
    public ResponseEntity postUser(@PathVariable("index") String index,@PathVariable("type") String type) throws  IOException{
       elasticSearchService.createIndexWithType(index, type);
        return  new ResponseEntity( HttpStatus.OK);
    }

    @DeleteMapping("/{index}")
    @ApiOperation(value="删除索引", notes="index 索引名称")
    public ResponseEntity deleteIndex(@PathVariable("index") String index){
        elasticSearchService.deleteIndex(index);
        return  new ResponseEntity( HttpStatus.OK);
    }

    @GetMapping("/inExists/{index}")
    @ApiOperation(value="索引是否存在", notes="index 索引名称")
    public ResponseEntity inExistsRequest(@PathVariable("index") String index){
        elasticSearchService.inExistsRequest(index);
        return  new ResponseEntity( HttpStatus.OK);
    }


//    @GetMapping("get/book/novel")
//    @ResponseBody
//    public ResponseEntity get(@RequestParam(name="id") String id){
//
//        if (id.isEmpty()){
//            new ResponseEntity( HttpStatus.NOT_FOUND);
//        }
//
//        GetResponse result= this.transportClient.prepareGet("book","novel",id)
//            .get();
//
//        if(!result.isExists()){
//            new ResponseEntity(result.getSource(), HttpStatus.NOT_FOUND);
//        }
//
//        return  new ResponseEntity(result.getSource(), HttpStatus.OK);
//    }


//    @PostMapping("post/book/novel")
//    @ResponseBody
//    public ResponseEntity save(@RequestParam(name = "name") String name,
//                               @RequestParam(name = "sex") String sex,
//                               @RequestParam(name = "age") Integer age,
//                               @RequestParam(name = "introduce") String introduce,
//                               @RequestParam(name = "birthday")@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")Date birthday){
//
//        System.out.println("name="+name);
//        System.out.println("sex="+sex);
//        System.out.println("age="+age);
//        System.out.println("introduce="+introduce);
//        System.out.println("birthday="+birthday);
//
//        try {
//            XContentFactory.jsonBuilder()
//                    .startObject()
//                    .field("title",name)
//                    .endObject();
//
//            IndexResponse result=this.transportClient.prepareIndex()
//                    .setSource()
//                    .get();
//            return  new ResponseEntity(result.getId(), HttpStatus.OK);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//
//
//
//
//    }

}

